package com.olap.starter.controller.admin;
import com.olap.starter.common.service.crud.CRUDService;
import com.olap.starter.common.util.ExcelUtil;
import com.olap.starter.common.vo.*;
import com.olap.starter.core.response.Result;
import com.olap.starter.core.response.ResultUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.Map;

/**
 * @author WBG
 * @date 2024/2/2 11:21
 * @describe
 */
@RestController
@RequestMapping("/online/crud")
public class CRUDController {

    @Autowired
    private CRUDService crudService;
    @PostMapping("/query")
    public Result query(@RequestBody QueryParameter queryParameter) {
        return ResultUtil.success(crudService.queryData(queryParameter));
    }

    @PostMapping("/queryFlux")
    public Flux<Map<String, Object>> queryFlux(@RequestBody QueryParameter queryParameter) {
        return crudService.queryFlux(queryParameter);
    }

    @PostMapping("/save")
    public Result save(@RequestBody SaveParameter saveParameter) {
        return crudService.save(saveParameter);
    }

    @PostMapping("/batchSave")
    public Result batchSave(@RequestBody BatchSaveParameter batchSaveParameter) {
        return crudService.batchSave(batchSaveParameter);
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam MultipartFile file) {
        try {
            return ResultUtil.success(ExcelUtil.WorkbookToListMap(ExcelUtil.importExcel(file)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("导入失败！" + e.getMessage());
        }
    }

    @PostMapping("/modify")
    public Result modify(@RequestBody ModifyParameter modifyParameter) {
        return crudService.modify(modifyParameter);
    }

    @PostMapping("/batchModify")
    public Result batchModify(@RequestBody BatchUpdateParameter batchUpdateParameter) {
        return crudService.batchModify(batchUpdateParameter);
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody Map<String, Object> params) {
        return crudService.delete(params);
    }

    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestBody DeleteParameterVue2 deleteParameter) {
        return crudService.batchDelete(deleteParameter);
    }
}
